<?php

namespace App\Notifications;

use App\Models\Admin\Admin;
use App\Models\Admin\AdminNotification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;

class SystemNotification extends Notification implements ShouldQueue
{
    use Queueable;

    public $level;
    public $title;
    public $content;
    /**
     * Create a new notification instance.
     */
    public function __construct($title,$content,$level=0)
    {
        $this->level = $level;
        $this->title = $title;
        $this->content = $content;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @return array<int, string>
     */
    public function via(object $notifiable): array
    {
        return ['database'];
    }


    public function toDatabase($notifiable)
    {
        //只需要处理管理员的通知
        if ($notifiable instanceof Admin || is_subclass_of($notifiable,Admin::class))
        {
            $record = new AdminNotification([
                "admin_id" => $notifiable->id,
                "level" => $this->level,
                "title" => $this->title,
                "content" => $this->content,
            ]);
            $notifiable->notification()->save($record);
        }

        return [
            'title' => $this->title,
            'content' => $this->content,
        ];
    }
}
